export class StringUtils {
  /**
   * 分割拼接符号
   */
  public static split = "；；";
  public static doHandleZero(value: number, length: number): string;
  public static doHandleZero(value: string, length: number): string;
  /**
   * 给值补0
   * @param value
   * @param length
   */
  public static doHandleZero(value: any, length: number): string {
    if (typeof value === "number") {
      value = String(value)
    }
    const val: string = value as string;
    if (val.length >= length) {
      return val
    }
    const i = length - val.length
    const repeat = StringUtils.repeat('0', i)
    return repeat + val
  }

  /**
   * 多次拼接
   * @param s 拼接的值
   * @param i 拼接次数
   */
  public static repeat(s: string, i: number): string {
    let result = ''
    for (let j = 0; j < i; j++) {
      result += s
    }
    return result
  }

  /**
   * 首字符大写
   * @param a
   */
  public static firstToUp(a: string): string {
    a = a[0].toUpperCase() + a.substring(1)
    return a
  }

  /**
   * 字符全匹配带忽略，ignoreArr里面的字符创也认为是需要匹配的
   * 但是是需要包含在需要匹配的字符中才匹配
   * // stringFullMatchAndIgnoreMatch(
   * //     '2019-441303-26-02--2-019-005879',
   * //     '2019',
   * //     ['-']
   * // );
   * @param str {String}
   * @param matchStr {String}
   * @param ignoreArr {String[]}
   * @return {{ startIndex: Number,
   *             endIndex: Number,
   *             match: Boolean,
   *             value: String}[]}
   */
  public static stringFullMatchAndIgnoreMatch(str, matchStr, ignoreArr = []) {
    let joinStr = ignoreArr.join('|')
    joinStr = '[' + joinStr + ']{0,}'
    joinStr = matchStr
      .split('')
      .join(joinStr)
    let reg = new RegExp(joinStr, 'g')
    let result = []
    str.replace(reg, ($0, $1) => {
      let item = {
        startIndex: $1,
        endIndex: $1 + $0.length,
        match: true,
        value: $0 || ''
      }
      result.push(item)
      return $0
    })
    let result2 = []
    for (let i = 1; i < result.length; i++) {
      let startIndex = result[i - 1].endIndex
      let endIndex = result[i].startIndex
      let s = str.substring(startIndex, endIndex)
      let item = {
        startIndex: startIndex,
        endIndex: s.length + startIndex,
        match: false,
        value: s || ''
      }
      result2.push(item)
    }
    result.push(...result2)
    result = result.sort((a, b) => {
      return a.startIndex - b.startIndex
    })
    if (result.length > 0) {
      let start = result[0]
      if (start.startIndex !== 0) {
        result.splice(0, 0, {
          startIndex: 0,
          endIndex: start.startIndex,
          match: false,
          value: str.substring(0, start.startIndex) || ''
        })
      }
      let last = result[result.length - 1]
      let resultLength = last.endIndex
      if (resultLength !== str.length) {
        result.push({
          startIndex: resultLength,
          endIndex: str.length,
          match: false,
          value: str.substring(resultLength) || ''
        })
      }
    } else {
      result.push({
        startIndex: 0,
        endIndex: str.length,
        match: false,
        value: str
      })
    }
    return result
  }
}
